package com.ccnu.JobHub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccnu.JobHub.common.util.Token;
import com.ccnu.JobHub.model.common.Result;
import com.ccnu.JobHub.model.database.StarRelation;
import com.ccnu.JobHub.model.database.Student;
import com.ccnu.JobHub.service.impl.StarRelationService;
import com.ccnu.JobHub.service.impl.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("follow")
@CrossOrigin("*")
@Slf4j
public class FollowController {
    private final StarRelationService starRelationService;

    private final StudentService studentService;

    public FollowController(StarRelationService starRelationService, StudentService studentService) {
        this.starRelationService = starRelationService;
        this.studentService = studentService;
    }

    /**
     * 关注用户
     * @param token token令牌
     * @param studentId1 关注的人
     * @return result结果
     */
    @PostMapping("follow")
    public Result follow(@RequestHeader("token") String token, @RequestBody Map<String,String > studentId1){
        log.info("--------------------关注用户请求--------------------");
        String studentId = Token.inspectOldToken(token);
        Integer  studentId2 = Integer.valueOf(studentId1.get("studentId"));

        QueryWrapper<StarRelation> starRelationQueryWrapper = new QueryWrapper<StarRelation>()
                .eq("student_id",studentId)
                .eq("star_student_id",studentId2);

        StarRelation starRelation = starRelationService.getOne(starRelationQueryWrapper);
        if (starRelation == null){
            StarRelation starRelation1 = new StarRelation();
            starRelation1.setStudentId(Integer.valueOf(studentId));
            starRelation1.setStarStudentId(studentId2);
            starRelation1.setRelation(1);
            starRelationService.save(starRelation1);
            return Result.success("关注用户成功");
        }
        else {
            return Result.error("已关注该用户");
        }


    }

    /**
     * 取消关注
     * @param token token令牌
     * @param studentId1 关注的学生id
     * @return Result结果
     */
    @PostMapping("unfollow")
    public Result unfollow(@RequestHeader("token") String token, @RequestBody Map<String,String > studentId1){
        log.info("--------------------取消关注请求--------------------");
        String studentId = Token.inspectOldToken(token);
        String studentId2 = studentId1.get("studentId");

        QueryWrapper<StarRelation> starRelationQueryWrapper = new QueryWrapper<StarRelation>()
                .eq("student_id",studentId)
                .eq("star_student_id",studentId2);
        StarRelation starRelation = starRelationService.getOne(starRelationQueryWrapper);
        if (starRelation == null){
            return Result.error("未关注该用户");
        }
        else {
            starRelationService.removeById(starRelation);
            return Result.success("取消关注成功");
        }

    }

    /**
     * 显示关系接口
     * @param token token令牌
     * @param studentId2 关注学生的id
     * @return Result结果
     */
    @GetMapping("relationship")
    public Result relationship(@RequestHeader("token") String token, @RequestParam("studentId") String studentId2){
        log.info("--------------------显示关系请求--------------------");
        String studentId = Token.inspectOldToken(token);

        QueryWrapper<StarRelation> starRelationQueryWrapper = new QueryWrapper<StarRelation>()
                .eq("student_id",studentId)
                .eq("star_student_id",studentId2);
        QueryWrapper<StarRelation> starRelationQueryWrapper2 = new QueryWrapper<StarRelation>()
                .eq("student_id",studentId2)
                .eq("star_student_id",studentId);
        StarRelation starRelation = starRelationService.getOne(starRelationQueryWrapper);
        StarRelation starRelation2 = starRelationService.getOne(starRelationQueryWrapper2);
        if (starRelation == null){
            return Result.success("显示关系成功",0);
        }
        else if (starRelation2 != null){
            return Result.success("显示关系成功",2);

        }
        else {
            return Result.success("显示关系成功",1);
        }

    }

    @GetMapping("showFollow")
    public Result showFollow(@RequestHeader("token") String token){
        log.info("--------------------显示关注列表请求--------------------");
        String studentId = Token.inspectOldToken(token);
        new Student();
        Student student;
        QueryWrapper<StarRelation> starRelationQueryWrapper = new QueryWrapper<StarRelation>()
                .eq("student_id",studentId);
        List<StarRelation> starRelations = starRelationService.list(starRelationQueryWrapper);
        List<Map<String , Object>> followMaps = new ArrayList<>();
        for(StarRelation starRelation : starRelations){
            student = studentService.getById(starRelation.getStarStudentId());
            Map<String , Object> follow = new HashMap<>();
            follow.put("studentId",student.getStudentId());
            follow.put("nickname",student.getNickname());
            follow.put("briefIntroduction",student.getBriefIntroduction());
            followMaps.add(follow);
        }
        return Result.success("显示关注列表成功",followMaps);
    }

    /**
     * 显示关注人数和粉丝数
     * @param studentId 学生学号
     * @return 一个Result结果
     */
    @GetMapping("showCountFollowAndFan")
    public Result showCountFollowAndFan(@RequestParam("studentId") String studentId){
        log.info("--------------------显示关注人数和粉丝数请求--------------------");
        QueryWrapper<StarRelation> starRelationQueryWrapper = new QueryWrapper<StarRelation>()
                .eq("student_id",studentId);
        QueryWrapper<StarRelation> starRelationQueryWrapper2 = new QueryWrapper<StarRelation>()
                .eq("star_student_id",studentId);
        Map<String ,Integer> result = new HashMap<>();
        result.put("follow", (int) starRelationService.count(starRelationQueryWrapper));
        result.put("fan", (int) starRelationService.count(starRelationQueryWrapper2));
        return Result.success("显示关注人数和粉丝数成功",result);
    }


}
